<?php

namespace App\Kuafu\WaiMai\Model;

use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Kuafu\Base\Model\BaseModel;

class WaiMaiShopPromiseSwitchModel extends BaseModel
{
    use KfUid;

    protected $connection = 'mysqlWaiMaiAll';
    protected $table      = 'waimai_shop_promise_switch'; //表名
    protected $primaryKey = 'id';
    public    $timestamps = true;

    protected $fillable = [
        'shopId',
        'kfPromiseTime',
        'elmPromiseSwitch',
        'elmPromiseTime',
        'silentSwitch',
    ];

    public function addData(int $shopId)
    {
        $arr = [];

        $defaultData = [
            'kfPromiseTime'   => WaiMaiDBEnum::KF_PROMISE_DEFAULT_TIME,
            'elmPromiseSwitch' => WaiMaiDBEnum::ELM_PROMISE_DEFAULT_SWITCH,
            'elmPromiseTime'   => 0,
            'silentSwitch'    => WaiMaiDBEnum::SILENT_DEFAULT_SWITCH,
        ];

        foreach ($defaultData as $field => $value) {
            if ($this->$field === null) {
                $arr[$field] = $value;
            } else {
                $arr[$field] = $this->$field;
            }
        }

        return self::query()->updateOrCreate(['shopId' => $shopId], $arr);
    }

    public static function getData(int $shopId)
    {
        return self::query()->where('shopId', $shopId)->first();
    }
}
